package dsl.client;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JOptionPane;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

/**
 * 通过图形界面与用户进行交互
 */
public class GUI {
    /**
     * 通过对话框传递给用户信息
     * 
     * @param str 要传递的信息
     */
    public static void showInfo(String str) {
        JOptionPane.showMessageDialog(null, str);
        return;
    }
}

/**
 * 带滚条的文本框，显示输入输出的内容
 */
class MyTextArea extends JScrollPane {
    public JTextArea textArea;

    public MyTextArea(int x, int y, int width, int height, Boolean editable) {
        textArea = new JTextArea();
        textArea.setBackground(Color.white);
        textArea.setCaretColor(Color.BLUE);
        textArea.setSelectionColor(Color.GREEN);
        textArea.setSelectedTextColor(Color.RED);
        textArea.setFont(new Font("宋体", Font.PLAIN, 16));
        textArea.setEditable(editable);
        textArea.setBorder(BorderFactory.createLoweredBevelBorder());
        textArea.setBounds(x, y, width, height);
        textArea.setLineWrap(true);

        this.setViewportView(textArea);
        this.setBounds(x, y, width, height);
        this.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
    }
}

/**
 * 对话框，enter键为输入结束标志
 */
class DialogBox {
    private static JFrame frame = new JFrame("智能客服");
    private JLayeredPane lp = new JLayeredPane();
    private JLabel lable1 = new JLabel("历史记录: ");
    private JLabel lable2 = new JLabel("请输入：");
    // 分别为输出框与输入框
    private static MyTextArea output = new MyTextArea(5, 20, 450, 400, false);
    private static MyTextArea input = new MyTextArea(5, 440, 450, 70, true);

    /**
     * 表示系统信息
     */
    public static final int SYSTEM_ID = 2;
    /**
     * 表示用户自己信息
     */
    public static final int CUSTOM_ID = 1;
    /**
     * 表示客服信息
     */
    public static final int CUSTOM_SERVICE_ID = 0;
    /**
     * 用户输入的信息
     */
    public String str = "";

    public DialogBox() {
        frame.setSize(475, 553);
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setBackground(Color.yellow);
        frame.setContentPane(lp);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        lable1.setBounds(15, 0, 100, 20);
        lable2.setBounds(15, 420, 100, 20);
        lp.add(output);
        lp.add(input);
        lp.add(lable1);
        lp.add(lable2);

        // 输入框对键盘的监听
        input.textArea.addKeyListener(new KeyListener() {

            @Override
            public void keyTyped(KeyEvent e) {
                // do nothing
            }

            @Override
            public void keyReleased(KeyEvent e) {
                // 释放按键后清除输入框中的内容
                if (e.getKeyChar() == 10)
                    input.textArea.setText("");
            }

            @Override
            public void keyPressed(KeyEvent e) {
                // 按下enter键后，若输入不为空，则获得输入
                if (e.getKeyChar() == 10) {
                    if (!input.textArea.getText().equals("")) {
                        str = input.textArea.getText();
                    }
                }
            }
        });
    }

    /**
     * 设置输出框中输出的内容。
     * 
     * @param text 输出的内容
     * @param id   用于区分现输入的角色
     */
    public void printInfo(String text, int id) {
        String str = "-----------------------------------------------\n";
        if (id == CUSTOM_SERVICE_ID)
            output.textArea.setText(output.textArea.getText() + "\n" + str + "Customer Service: " + text);
        else if (id == CUSTOM_ID)
            output.textArea.setText(output.textArea.getText() + "\n" + str + "You: " + text);
        else if (id == SYSTEM_ID)
            output.textArea.setText(output.textArea.getText() + "\n" + str + "System: " + text);
        output.textArea.setCaretPosition(output.textArea.getText().length());
    }

    /**
     * 关闭对话框
     */
    public void close() {
        frame.dispose();
    }
}
